<template>
  <div>
    <h1>会议室管理</h1>
    <el-card class="mt-10" shadow="hover">
      <div class="flex-c">
        <div>
          <span>会议室名称:</span>
          <el-input v-model="searchParams.name" style="width: 240px" class="ml-10" clearable />
        </div>
        <div class="ml-20">
          <span>容纳人数:</span>
          <el-input v-model="searchParams.capacity" style="width: 240px" class="ml-10" clearable />
        </div>
        <div class="ml-20">
          <span>设备:</span>
          <el-input v-model="searchParams.equipment" style="width: 240px" class="ml-10" clearable />
        </div>
        <el-button type="primary" class="ml-30" @click="querySearch">查询</el-button>
        <el-button type="primary" class="ml-10" @click="addOrEditMeetingRoom(null)">添加</el-button>
      </div>
    </el-card>
    <el-card class="mt-15" shadow="hover">
      <div class="f-bold f-18">用户列表</div>
      <el-table :data="tableData" stripe style="width: 100%" class="mt-10">
        <el-table-column prop="name" label="名称" width="180" />
        <el-table-column prop="capacity" label="容纳人数" width="180" />
        <el-table-column prop="location" label="位置" />
        <el-table-column prop="equipment" label="设备" />

        <el-table-column prop="description" label="描述" />
        <el-table-column prop="createTime" label="添加时间" />
        <el-table-column prop="updateTime" label="上次更新时间" />
        <el-table-column prop="updateTime" label="预定状态">
          <template #default="scope">
            <el-tag type="danger" v-if="scope.row.isBooked">已被预订</el-tag>
            <el-tag type="success" v-else>可预定</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="action" label="操作" width="180">
          <template #default="scope">
            <div class="flex">
              <el-button type="primary" size="small" @click="addOrEditMeetingRoom(scope.row)"
                >修改</el-button
              >
              <el-button type="primary" size="small" @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-row justify="end" class="mt-15">
        <el-pagination
          background
          layout="prev, pager, next,total"
          :total="total"
          v-model:page-size="searchParams.pageSize"
          v-model:current-page="searchParams.pageNo"
          @change="changePage"
        />
      </el-row>
    </el-card>

    <!-- 添加/修改会议室 -->
    <CreateMeetingRoomModal
      v-if="isShoModal"
      v-model="isShoModal"
      @on-submit="onSubmit"
      :info="modalInfo"
    />
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { meetingRoomList, deleteMeetingRoom, createMeetingRoom, updateMeetingRoom } from '@/api'
import { ElMessage, ElMessageBox } from 'element-plus'
import CreateMeetingRoomModal, { type IMeetingRoomFormData } from './CreateMeetingRoomModal.vue'

defineOptions({
  name: 'meetingRoomManage'
})

export interface SearchMeetingRoom {
  name: string
  capacity: number | string
  equipment: string
  pageNo: number
  pageSize: number
}

export interface IMeetingRoomResult {
  id: number
  name: string
  capacity: number
  location: string
  equipment: string
  description: string
  isBooked: boolean
  createTime: Date
  updateTime: Date
}

const searchParams = reactive<SearchMeetingRoom>({
  name: '',
  capacity: '',
  equipment: '',
  pageNo: 1,
  pageSize: 10
})

let tableData = ref<IMeetingRoomResult | null>(null)
let total = ref(0)
const isShoModal = ref(false)
const modalInfo = ref<IMeetingRoomResult | null>()

onMounted(() => {
  search()
})

const search = async () => {
  const res = await meetingRoomList(searchParams)
  if (res.status === 200 || res.status === 201) {
    const { data } = res.data
    tableData.value = data.list || []
    total.value = data.total || 0
  }
}

const querySearch = () => {
  searchParams.pageNo = 1
  search()
}

const changePage = (currentPage: number, pageSize: number) => {
  searchParams.pageNo = currentPage
  searchParams.pageSize = pageSize
  search()
}

const handleDelete = (row: IMeetingRoomResult) => {
  ElMessageBox.confirm('确认删除吗?删除后不可恢复', '提醒', {
    confirmButtonText: '确认',
    cancelButtonText: '关闭',
    type: 'warning'
  })
    .then(async () => {
      const res = await deleteMeetingRoom(row.id)
      if (res.status === 200 || res.status === 201) {
        ElMessage.success('操作成功')
        search()
      }
    })
    .catch(() => {
      console.log('弹窗关闭')
    })
}

const addOrEditMeetingRoom = async (data: IMeetingRoomResult | null) => {
  modalInfo.value = data || null
  isShoModal.value = true
}
const onSubmit = async (params: IMeetingRoomFormData) => {
  if (!params) return
  const api = params.id ? updateMeetingRoom : createMeetingRoom

  const res = await api(params)
  const { code, data } = res.data
  if (code === 201 || code === 200) {
    ElMessage.success('创建成功')
    search()
    isShoModal.value = false
  } else {
    ElMessage.error(data)
  }
}
</script>

<style scoped></style>
